package com.lib.image.preview

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.lib.image.R

/**
 * 封面重新选择
 * @author shalei
 */
class CoverSelectionActivity: AppCompatActivity() {

    companion object {
        private const val KEY_BUNDLE = "key_bundle"
        private const val KEY_PATH_LIST = "key_path_list"
        private const val KEY_INDEX = "key_index"

        @JvmStatic
        fun newIntent(activity: Activity, pathList: Array<String>, index: Int): Intent {
            val intent = Intent(activity, CoverSelectionActivity::class.java)
            val bundle = Bundle()
            bundle.putStringArray(KEY_PATH_LIST, pathList)
            bundle.putInt(KEY_INDEX, index)
            intent.putExtra(KEY_BUNDLE, bundle)
            return intent
        }
    }

    private lateinit var mViewModel: CoverViewModel

    //按下返回键 可以回退fragment 栈中的fragment
    override fun onSupportNavigateUp() = Navigation.findNavController(this, R.id.nav_cover_selection).navigateUp()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_cover_selection)
        mViewModel = ViewModelProvider(this).get(CoverViewModel::class.java)
        val bundle = intent.getBundleExtra(KEY_BUNDLE)!!
        val index = bundle.getInt(KEY_INDEX)
        val path = bundle.getStringArray(KEY_PATH_LIST)!!
        mViewModel.getCoverSelection().value = CoverSelection(path, index)
    }

}